package com.tuanzi.loan.web.bean.collaborate;

import com.tuanzi.loan.business.entity.collaborate.CollaborateChannelRatio;
import com.tuanzi.loan.business.service.collaborate.CollaborateOldChannelRatioService;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Objects;

/**
 * User: liangZongXi
 * Create time: 11:57 2019/5/17
 * Description:
 */

@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CollaborateOldManagerRatioBean extends JSFInfoBean<CollaborateChannelRatio> {
    private static final long serialVersionUID = 4803249192354166565L;

    private final CollaborateOldChannelRatioService collaborateOldChannelRatioService;

    private List<CollaborateChannelRatio> channels;

    @Autowired
    public CollaborateOldManagerRatioBean(CollaborateOldChannelRatioService collaborateOldChannelRatioService) {
        this.collaborateOldChannelRatioService = collaborateOldChannelRatioService;
    }

    @PostConstruct
    private void init(){
        channels = collaborateOldChannelRatioService.findAllIncludeChannelName();
        $ = new CollaborateChannelRatio();
    }

    public void save(){
        Integer id = $.getId();
        Integer ratio = $.getRatio();
        if(null != id && null != ratio){
            CollaborateChannelRatio one = collaborateOldChannelRatioService.findOne(id);
            if(null != one){
                one.setRatio(ratio);
                collaborateOldChannelRatioService.saveOrUpdate(one);
                addSuccessfullyMessage();
                return;
            }
        }

        addErrorMessage("所需参数不能为空");
    }

    public void channelChange(){
        for (CollaborateChannelRatio ratio : channels) {
            if(Objects.equals($.getId(), ratio.getId())){
                $.setRatio(ratio.getRatio());
                break;
            }
        }
    }


}
